﻿using FPC.Offer.UI.SkyDrive;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace FPC.Offer.UI.GeneralProcessor.SkyDriveHand
{
    /// <summary>
    /// FileHandler 的摘要说明
    /// </summary>
    public class FileHandler : IHttpHandler
    {
        FPC.Offer.Service.System_SubfolderTableService Sky = new Service.System_SubfolderTableService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            HttpPostedFile file = context.Request.Files["pic"];
            var FileName = context.Request["FName"];
            var name = context.Request["name"];

            string path = System.AppDomain.CurrentDomain.BaseDirectory + name + "\\" + FileName;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            int daxiao = file.ContentLength;
            string filename = file.FileName; //获得上传文件全路径 

            int place = filename.LastIndexOf(".") + 1; //获得文件扩展名的位置 
            string extname = filename.Substring(place).ToLower(); //获得上传文件的扩展名，全部转换为小写的

            string allowedType = "jpg|mp3|mp4|rar|rmvb|vob|txt|docx|doc";//允许上传的文件类型

            long allowedSize = 512000 * 1024;//允许上传的饿文件大小，单位是字节（byte），现在默认的我设置的是500M，你自己改成你需要的。

            if (allowedType.Contains(extname) && file.ContentLength < allowedSize)
            {

                string filePath = path + "\\" + filename;//上传文件的存放路径和文件的名称，我这里就是用文件原来的名字了，你可以更改为其他的名字，如加时间的就是：path+DateTime.Now+filename 
                if (Directory.Exists(filePath))
                {
                    context.Response.Write("<script>location.href='../SkyDrive/wangpanindex.aspx/'</script>");
                    return;
                }
                else
                {
                    Model.Subfolder s = new Model.Subfolder();
                    s.SName = filename;
                    s.SUrl = filePath;
                    s.SMemory = daxiao.ToString() + "B";
                    s.NetState = 1;
                    s.FileDate = DateTime.Now;
                    s.MemorySum = "500m";
                    s.FilePwd = "1234";
                    s.FileName = FileName;

                    int a = Sky.Add(s);

                    file.SaveAs(filePath); //文件上传

                    context.Response.Write("上传成功");
                    return;
                }

            }
            else
            {
                context.Response.Write("文件上传失败，估计是格式不支持或者文件太大啦");
                return;
            }





        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}